import chardet
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook

results = []
base_url = 'http://www.tcmap.com.cn/hubei/'
html1 = requests.get(base_url + 'yichangshi.html')
encoding = chardet.detect(html1.content)['encoding']
print(f"返回编码为: "+encoding)
html1.encoding = encoding
soup1 = BeautifulSoup(html1.text, "html.parser")
page_left1 = soup1.find(id="page_left")
list1 = page_left1.find_all("table")
list2 = list1[3]
list3 = list2.find_all("tr")
for index3, item3 in enumerate(list3):
    if index3 == 0:
        continue
    list4 = item3.find_all("td")
    key1 = ''
    key2 = ''
    for index4, item4 in enumerate(list4):
        if index4 == 0:
            key1 = item4.text
        elif index4 == 1:
            key2 = item4.text
        elif index4 == 3:
            list5 = item4.find_all('div', class_=['l', 'l2', 'le'])
            for item5 in list5:
                item6 = item5.find("a")
                key3 = item6.text

                html2 = requests.get(base_url + item6['href'])
                html2.encoding = encoding
                soup2 = BeautifulSoup(html2.text, "html.parser")
                page_left2 = soup2.find(id="page_left")
                list7 = page_left2.find_all("table")
                key4 = ''
                for index7, item7 in enumerate(list7):
                    if index7 == 1:
                        list8 = item7.find_all("tr")
                        list9 = list8[1].find_all("td")
                        str9 = list9[0].text
                        key4 = str9.replace('区划代码：', '')
                    elif index7 == 3:
                        list10 = item7.find_all("tr")
                        for index10, item10 in enumerate(list10):
                            if index10 != 0:
                                list11 = item10.find_all("td")
                                key5 = ''
                                key6 = ''
                                for index11, item11 in enumerate(list11):
                                    if index11 == 0:
                                        key5 = item11.text
                                    elif index11 == 1:
                                        key6 = item11.text.replace('～', key4)

                                results.append([key1, key2, key3, key4, key5, key6])

print(results)
# 判断集合是否为空
print(len(results))
if(len(results) == 0) :
    print('没有数据')
    exit()

print('>>>>>>写入Exel中，请等待...')

wb = Workbook()  # 新建工作簿
ws = wb.active  # 获取工作表

# 写入Excel表头
ws.append(['区域名称', '区域编码', '街道名称', '街道编码', '社区名称', '社区编码'])  # 表头
print('>>>>>>写入Excel表头')

# 写入Excel内容
for i in range(0, len(results)):
    ws.append(results[i])

# 保存Excel
print('>>>>>>Excel保存中，请耐心等待...')
wb.save(r'社区.xlsx')  # 保存到指定路径，保存的文件必须不能处于打开状态，因为文件打开后文件只读
print('>>>>>>写入Exel完成')


